package vistas;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.JMenuItem;

import clases.Gradosacademicos_class;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.Cursor;
import javax.swing.JScrollPane;
import java.awt.Toolkit;

public class Buscar_gradoacademicoform extends JInternalFrame {

	private final JPanel contentPanel = new JPanel();
	private JTextField tbuscar;
	private JTable tabla_gradoacademico;
	private int filas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Buscar_gradoacademicoform dialog = new Buscar_gradoacademicoform();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Buscar_gradoacademicoform() {
		setFrameIcon(new ImageIcon(Asignarhorarios_buscarform.class.getResource("/icons/extra.png")));
		setClosable(true);
		setIconifiable(true);
		setTitle("Buscar Grado Acad\u00E9mico");
		filas=0;
		setBounds(100, 100, 615, 463);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("Nombres");
			label.setFont(new Font("Tahoma", Font.PLAIN, 13));
			label.setBounds(10, 13, 59, 16);
			contentPanel.add(label);
		}
		{
			tbuscar = new JTextField();
			tbuscar.setFont(new Font("Tahoma", Font.PLAIN, 13));
			tbuscar.setColumns(10);
			tbuscar.setBounds(79, 11, 385, 20);
			contentPanel.add(tbuscar);
		}
		{
			JButton button = new JButton("Buscar");
			button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			button.setContentAreaFilled(false);
			button.setIcon(new ImageIcon(Buscar_gradoacademicoform.class.getResource("/icons/buscar.png")));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					metodoBuscar();
				}
			});
			button.setBounds(481, 11, 108, 23);
			contentPanel.add(button);
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 40, 579, 374);
			contentPanel.add(scrollPane);
			{
				tabla_gradoacademico = new JTable();
				scrollPane.setViewportView(tabla_gradoacademico);
				tabla_gradoacademico.setModel(new DefaultTableModel(
					new Object[][] {
					},
					new String[] {
						"codigogrado", "Nombre Grado"
					}
				));
				tabla_gradoacademico.getColumnModel().getColumn(0).setPreferredWidth(15);
				tabla_gradoacademico.getColumnModel().getColumn(0).setMinWidth(0);
				tabla_gradoacademico.getColumnModel().getColumn(0).setMaxWidth(0);
				{
					JPopupMenu popupMenu = new JPopupMenu();
					addPopup(tabla_gradoacademico, popupMenu);
					{
						JMenuItem mntmModificar = new JMenuItem("Modificar");
						mntmModificar.setIcon(new ImageIcon(Buscar_gradoacademicoform.class.getResource("/icons/modificar_pq.png")));
						mntmModificar.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								if(tabla_gradoacademico.getSelectedRow()>-1)
								{
									Ingresar_gradoacademicoform grado = new Ingresar_gradoacademicoform(tabla_gradoacademico.getValueAt(tabla_gradoacademico.getSelectedRow(), 0).toString());
									Principal.contenedor.add(grado);
									grado.setVisible(true);
								}
							}
						});
						popupMenu.add(mntmModificar);
					}
				}
			}
			DefaultTableModel mgrados = (DefaultTableModel)tabla_gradoacademico.getModel();
			try
			{
				ResultSet resultado = new Gradosacademicos_class().mostrar();
				while(resultado.next())
				{
					mgrados.addRow(new Object[filas]);
					mgrados.setValueAt(resultado.getString("codigogrado"), filas, 0);
					mgrados.setValueAt(resultado.getString("grado"), filas, 1);
					filas++;
				}
				resultado.close();
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(null, ex.toString());
			}
		}
		
		metodoBuscar();
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	private void metodoBuscar()
	{

		try
		{
			String query = "SELECT * FROM gradosacademicos WHERE grado LIKE ?";
			ArrayList<String> parametros = new ArrayList<String>();
			parametros.add("%"+tbuscar.getText()+"%");	
			ResultSet resultado = new Gradosacademicos_class().mostrar(query, parametros);
			DefaultTableModel tgrados = (DefaultTableModel)tabla_gradoacademico.getModel();
			while(tgrados.getRowCount()>0) tgrados.removeRow(0);
			int filas=0;
			
			while(resultado.next())
			{
				tgrados.addRow(new Object[filas]);
				tgrados.setValueAt(resultado.getString("codigogrado"), filas, 0);
				tgrados.setValueAt(resultado.getString("grado"), filas, 1);
				filas++;
			}
			resultado.close();
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, ex.toString());
		}
	
	}
}
